package com.pic.mgt;

import org.apache.commons.lang.StringUtils;

import com.pic.mgt.config.ConfigBean;
import com.pic.mgt.config.ConfigurationException;
import com.pic.mgt.utils.NumericUtils;

/**
 * [Bean:存放系統參數設定值]
 * @author 洪政緯
 *
 */
public class SystemConfigBean implements ConfigBean {
	private boolean autoCreateFolder;
	int businessRetryTimes;
	int businessRetryInterval;
	private String confirmationFilenameEndWith;
	private int targetFileLength;
	// TODO 只保留以上的設定值,以下的已經不是共用的設定值了
	private int bizTypeAtChar;
	private int executiontTimeStartFromChar;
	private int executionTimeEndtoChar;
	private int serialNumberStartFromChar;
	private int serialNumberEndToChar;
	private int terminalFormatLength;
	private int bizNameStartFromChar;
	private int bizNameEndToChar;
	
	/**
	 * 利用建構式來注入參數，不使用Setter。
	 * 主要原因是不允許散自變更設定值，如要變更則表示需從設定檔做變更，
	 * 並再建立一個新的 HaishinConfigBean
	 * @param businessRetryTimes 業務檔案錯誤重試次數
	 * @param businessRetryInterval 業務檔案錯誤重試(毫秒)
	 * @param confirmationFilenameEndWith 確認檔檔名結尾
	 * @param targetFileLength 目標檔案長度 
	 * @param BiztypeAtChar 集配信區別字元於確認檔檔名的位置
	 * @param executiontTimeStartFromChar 執行時間位於確認檔檔名的起始位置
	 * @param executionTimeEndtoChar 執行時間位於確認檔檔名的結束位置
	 * @param serialNumberStartFromChar 業務序號位於確認檔檔名的起始位置
	 * @param serialNumberEndToChar 業務序號位於確認檔檔名的結束位置
	 * @param terminalFormatLength  端末長度定義
	 * @param bizNameStartFromChar 業務名稱位於確認檔檔名的起始位置
	 * @param bizNameEndToChar 業務名稱位於確認檔檔名的結束位置
	 */
	public SystemConfigBean(int businessRetryTimes,
							int businessRetryInterval,
							String confirmationFilenameEndWith,
							int targetFileLength,
							int BiztypeAtChar,
							int serialNumberStartFromChar,
							int serialNumberEndToChar,
							int terminalFormatLength,
							int bizNameStartFromChar,
							int bizNameEndToChar
							) {
		this.autoCreateFolder = autoCreateFolder;
		this.businessRetryTimes = businessRetryTimes;
		this.businessRetryInterval = businessRetryInterval;
		this.confirmationFilenameEndWith = confirmationFilenameEndWith;
		this.targetFileLength = targetFileLength;
		this.bizTypeAtChar = BiztypeAtChar;
		//this.executiontTimeStartFromChar = executiontTimeStartFromChar;
		//this.executionTimeEndtoChar = executionTimeEndtoChar;
		this.serialNumberStartFromChar = serialNumberStartFromChar;
		this.serialNumberEndToChar = serialNumberEndToChar;
		this.terminalFormatLength = terminalFormatLength;
		this.bizNameStartFromChar = bizNameStartFromChar;
		this.bizNameEndToChar = bizNameEndToChar;
	}

	@Override
	public void checkConfig() throws ConfigurationException {		
		ConfigurationException exception =
			new ConfigurationException(
					"Haishin configuration setting error: can't be empty.");		
		// 檢查參數設定是否為null或empty		
		if (StringUtils.isBlank(this.confirmationFilenameEndWith)){throw exception;}		
		exception = null;
		
		// 檢查參數是否為負值
		if (NumericUtils.isPositive(businessRetryTimes)) {
			throw new ConfigurationException(
					"'SYSTEM.BUSINESS_FILE_RETRY_TIMES' can not be negative.");
		}
		if (NumericUtils.isPositive(businessRetryInterval)) {
			throw new ConfigurationException(
					"'SYSTEM.BUSINESS_FILE_RETRY_INTERVAL' can not be negative.");
		}
		if (NumericUtils.isPositive(targetFileLength)) {
			throw new ConfigurationException(
					"'HAISHIN.FORMAT.TARGET.FILE.LENGTH' can not be negative.");
		}
		if (NumericUtils.isPositive(bizTypeAtChar)) {
			throw new ConfigurationException(
					"'HAISHIN.FORMAT.HAISHIN.TYPE.ATCHAR' can not be negative.");
		}
		
		// 其它規則驗證
		if (bizTypeAtChar > targetFileLength) {
			throw new ConfigurationException(
			"'HAISHIN.FORMAT.HAISHIN.TYPE.ATCHAR' can not greater than 'SYSTEM.BUSINESS.TARGET.FILE.LENGTH'");
		}
	}

	public boolean isAutoCreateFolder() {
		return autoCreateFolder;
	}

	public int getBusinessRetryTimes() {
		return businessRetryTimes;
	}

	public int getBusinessRetryInterval() {
		return businessRetryInterval;
	}
	
	public String getConfirmationFilenameEndWith() {
		return confirmationFilenameEndWith;
	}

	public int getTargetFileLength() {
		return targetFileLength;
	}

	public int getBizTypeAtChar() {
		return bizTypeAtChar;
	}

	public int getTerminalFormatLength() {
		return terminalFormatLength;
	}

	public int getExecutiontTimeStartFromChar() {
		return executiontTimeStartFromChar;
	}

	public int getExecutionTimeEndtoChar() {
		return executionTimeEndtoChar;
	}

	public int getSerialNumberStartFromChar() {
		return serialNumberStartFromChar;
	}

	public int getSerialNumberEndToChar() {
		return serialNumberEndToChar;
	}

	public int getBizNameStartFromChar() {
		return bizNameStartFromChar;
	}

	public int getBizNameEndToChar() {
		return bizNameEndToChar;
	}
}
